//
//  GXCycleImages.m
//  轮播图
//
//  Created by Snow on 16/7/21.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "GXCycleImages.h"

@interface GXCycleImages ()<UIScrollViewDelegate>
//私有属性对外公开 使用readonly 一般用来让别人修改这个属性的属性
@property (nonatomic ,strong)UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagesPage;
@end

@implementation GXCycleImages
#pragma mark - ScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page - 1;
    if (page == 0) {
        //改变滚动视图的偏移量
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _imagesPage.currentPage = _imagesArray.count - 3;
    } else if (page == _imagesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
    }
}
#pragma mark - initWithFrame
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //子控件初始化时 frame不要与初始化方法的frame一样 推荐使用self.bounds 或者CGRectMake (0, 0, frame.size.width, frame.size.width)
        [self initImagePage:frame];
        [self addSubview:_imagesPage];
     
    }
    return self;
}
#warning 后期需要添加page的位置改变 这里是默认位置
- (void)initImagePage:(CGRect)frame
{
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _imagesPage.numberOfPages = 5;
    _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 20 / 2);
     [self initCycleImages];
    [_imagesPage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
}
- (void)imagePageChange:(UIPageControl *)page
{
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}
- (void)initCycleImages
{
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
    [self addSubview:_cycleScrollView];
}
#pragma mark - ImagesArray
- (void)setImagesWithArray:(NSArray *)array
{
    //1.根据传过来的数组,进行处理,生成图片数组
    NSArray *imagesArray = [self handleArrayWithArray:array];
    //2.逻辑判断时 需要用到的数组内容,将数组赋成属性
    self.imagesArray = [NSArray arrayWithArray:imagesArray];
    //3.根据数组生成对应的ImageView
    [self createAllImagesViewsWithImageArray:imagesArray];
    // 4.根据数组创建相应视图,使用数组创建ImageView
        _imagesPage.numberOfPages = array.count;
    //因为添加了最后一张图, 要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    _cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
}
- (void)createAllImagesViewsWithImageArray:(NSArray *)imagesArray
{
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
- (NSArray *)handleArrayWithArray:(NSArray *)array
{
    //判断类型(不知道别人使用你的类的时候传进来的类型是什么)
    id firstObject = [array firstObject];
    //把最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    //判断对象是不是某种类型 isKindOfclass
    if ([firstObject isKindOfClass:[NSString class]]) {
        //把字符串数组 转成图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        //直接使用数组创建ImageView
        [imagesArray setArray:array];
    }
    //第一张图
    UIImage *first = [imagesArray firstObject];
    //最后一张图
    UIImage *last = [imagesArray lastObject];
    //    下标为0的位置放最后一张图
    [imagesArray insertObject:last atIndex:0];
    //    在数组最后添加第一张图
    [imagesArray addObject:first];
    return [imagesArray copy];
}

@end
